require("../css/register.less")
const CaptchaMini = require("../lib/captcha-mini.js")//验证插件

document.ready(function () {
    /*不传值，统一走默认值*/
    let captcha1 = new CaptchaMini({
        // lineNum:50,
        // dotR: 2,          //点的半径
        // dotNum: 85,       //点的数量
    })
    //定义一个全局变量来保存验证码
    let code = "";
    captcha1.draw(document.querySelector('#captcha1'), r => {
        // console.log(r, '验证码1');
        code = r
    });

    //  获取操作元素
    let phoneInp = document.querySelector("#phoneInp")//手机号
    let captchaInp = document.querySelector("#captchaInp")//验证码
    let passwordInp = document.querySelector("#passwordInp")//密码
    let passwordAgainInp = document.querySelector("#passwordAgainInp")//确认密码
    let login_regbtnInp = document.querySelector("#login_regbtnInp")//注册
    let tips = document.querySelector("#tips");//输入提示

    //注册事件
    login_regbtnInp.addEventListener("click", function () {
        //获取手机号
        let phone = phoneInp.value
        //获取验证码
        let captcha = captchaInp.value
        //获取密码
        let password = passwordInp.value
        //获取再次输入密码
        let passwordAgain = passwordAgainInp.value

        //验证表单
        if (!window.$utils.isPhone(phone)) {
            //textContent返回节点文本内容
            tips.textContent = "请输入正确的手机号码"
        } else if (captcha.toLowerCase() != code.toLowerCase()) {
            tips.textContent = "验证码输入有误"
        } else if (!window.$utils.isPassword(password)) {
            tips.textContent = "请输入6-12位的字母数字下划线密码";
        } else if (password != passwordAgain) {
            tips.textContent = "两次密码不一致,请重新输入"
        } else {
            //清空提示
            tips.textContent = "";

            //发送AJAX请求,进行注册
            axios.post("http://139.9.177.51:8099/users/add",
                {
                    "account": phone,
                    "password": password
                })
                .then(function (res) {
                    if (res.data.status === 0) {
                        //注册成功
                        alert("注册成功")
                        //跳转到登陆页
                        window.location.href = "./login.html"
                    } else if (res.data.status === 5001) {
                        //注册失败,请重新注册
                        alert("注册失败,请重新注册")
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
        }
    })
})